package ops.weixin.common.util.http;

import org.apache.http.Header;
import org.apache.http.client.methods.CloseableHttpResponse;

import ops.weixin.common.error.WxError;
import ops.weixin.common.error.WxErrorException;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * http框架的response代理类，
 * 存在其他Http请求时，方便提取公共方法
 * @author SuM
 * @date: 2019年12月24日下午2:53:59
 */
public class HttpResponseProxy {
  private static final Pattern PATTERN = Pattern.compile(".*filename=\"(.*)\"");

  private CloseableHttpResponse apacheHttpResponse;

  public HttpResponseProxy(CloseableHttpResponse apacheHttpResponse) {
    this.apacheHttpResponse = apacheHttpResponse;
  }

  public String getFileName() throws WxErrorException {
    //由于对象只能由一个构造方法实现，因此三个response对象必定且只有一个不为空
    if (this.apacheHttpResponse != null) {
      return this.getFileName(this.apacheHttpResponse);
    }

    //cannot happen
    return null;
  }

  private String getFileName(CloseableHttpResponse response) throws WxErrorException {
    Header[] contentDispositionHeader = response.getHeaders("Content-disposition");
    if (contentDispositionHeader == null || contentDispositionHeader.length == 0) {
      throw new WxErrorException(new WxError().setErrorMsg("无法获取到文件名").setErrorCode(99999));
    }

    return this.extractFileNameFromContentString(contentDispositionHeader[0].getValue());
  }

  private String extractFileNameFromContentString(String content) throws WxErrorException {
    if (content == null || content.length() == 0) {
      throw new WxErrorException(new WxError().setErrorMsg("无法获取到文件名").setErrorCode(99999));
    }

    Matcher m = PATTERN.matcher(content);
    if (m.matches()) {
      return m.group(1);
    }

    throw new WxErrorException(new WxError().setErrorMsg("无法获取到文件名").setErrorCode(99999));
  }

}
